package com.hilihili.controller.detailComponent;

import com.hilihili.domain.dto.DanmakuInfoDTO;
import com.hilihili.domain.dto.ShieldDanmukaDTO;
import com.hilihili.domain.entity.Danmaku;
import com.hilihili.domain.entity.Video;
import com.hilihili.service.IDanmakuService;
import com.hilihili.service.IVideoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * 评论Controller
 *
 * @author xhc
 * @date 2023-11-15
 */
@RestController
@RequestMapping("/hilihili/danmaku")
public class DanmakuController extends BaseController {
    @Autowired
    private IDanmakuService danmakuService;
    @Autowired
    IVideoService videoService;


    @GetMapping("/randomCreateDanmaku")
    public void randomCreateDanmaku() {
        Random random = new Random();
        List<Video> list = videoService.selectAll();
        list.forEach(e -> {
            for (int i = 0; i < 100; i++) {
                int randomInt = random.nextInt(100000000);
                Danmaku danmaku = new Danmaku();
                danmaku.setVideoId(e.getId());
                danmaku.setUserId(105L);
                danmaku.setTime(randomInt % 30L);
                danmaku.setTalk(randomInt + "");
                danmaku.setCreatedAt(LocalDateTime.now());
                danmaku.setUpdatedAt(LocalDateTime.now());
                danmaku.setCreateTime(new Date());
                danmaku.setUpdateTime(new Date());
                danmakuService.insertDanmaku(danmaku);
            }
        });
    }


    /**
     * 获取发送时间大于当前播放时间的视频弹幕
     *
     * @param id 视频id
     * @return {@link AjaxResult}
     */
    @PostMapping(value = "/getDanmakuByvideo")
    public AjaxResult getDanmakuListByVideo(@RequestBody DanmakuInfoDTO danmakuInfoDTO) {
        return success(danmakuService.selectDanmakuListById(danmakuInfoDTO));
    }

    /**
     * 获取视频所有弹幕
     *
     * @param videoId 视频id
     * @return {@link AjaxResult}
     */
    @GetMapping(value = "/getDanmakuByVideoId")
    public AjaxResult getDanmakuListByVideoId(String videoId) {
        return AjaxResult.success(danmakuService.selectDanmakuList(videoId));
    }


    /**
     * 新增弹幕
     *
     * @param danmaku 弹幕实体类
     * @return {@link AjaxResult}
     */
    @PostMapping("addDanmaku")
    public AjaxResult addDanmaku(@RequestBody Danmaku danmaku) {
        return toAjax(danmakuService.insertDanmaku(danmaku));
    }


    /**
     * 用户屏蔽弹幕
     * @param shieldDanmukaDTO
     * @return {@link AjaxResult}
     */
    @PostMapping("shieldUserDanmuka")
    public AjaxResult shieldUserDanmuka(@RequestBody ShieldDanmukaDTO shieldDanmukaDTO){
        return danmakuService.shieldUserDanmuka(shieldDanmukaDTO);
    }

    /**
     * 用户取消屏蔽用户弹幕
     * @param shieldDanmukaDTO
     * @return {@link AjaxResult}
     */
    @PostMapping("cancelShieldUserDanmuka")
    public AjaxResult cancelShieldUserDanmuka(@RequestBody ShieldDanmukaDTO shieldDanmukaDTO){
        return danmakuService.cancelShieldUserDanmuka(shieldDanmukaDTO);
    }

    /**
     * 用户获取屏蔽弹幕用户
     * @param userId
     * @return {@link AjaxResult}
     */
    @GetMapping("getShieldDanmukaList")
    public AjaxResult getShieldDanmukaList(Long userId){
        return danmakuService.getShieldDanmukaList(userId);
    }
}
